package org.roulette.core;

import java.util.ArrayList;
import java.util.List;
import org.roulette.strategy.BetProposal;
import org.roulette.strategy.BetStrategy;
import org.roulette.utils.BetConstant;
import org.roulette.utils.RouletteType;

/**
 *
 * @author hudimar
 */
public class Better implements WheelListener, WinListener{
	private Wheel wheel;
	private int money = 0;
	private BetStrategy strategy;
	private List<Bet> history = new ArrayList<Bet>();

	public Better(Wheel wheel, int money, BetStrategy strategy) {
		setWheel(wheel);
		this.money = money;
		setBetStrategy(strategy);
	}

	public void setWheel(Wheel wheel){
		if(this.wheel != null)
			this.wheel.removeListener(this);
		this.wheel = wheel;
		wheel.addListener(this);
	}

	public void setBetStrategy(BetStrategy strategy){
		this.strategy = strategy;
		strategy.setHistoryBets(history);
		strategy.setWheelHistory(wheel.getHistory());
	}

	public Bet placeNextBet(RouletteType type) {
		Bet bet = null;
		if(strategy.isNextBet())
		{
			bet = getNextBet(type);
		}else
		{
			System.out.println("Strategy doesn't generate new number.");
			bet = new Bet(BetConstant.none, 0, this);
		}
		return bet;
	}

	private Bet getNextBet(RouletteType type){
		BetProposal proposal = strategy.getNextBet(type);
		if(proposal.getBetConstant() != BetConstant.none)
			money -= proposal.getAmount();
		Bet bet = new Bet(proposal.getBetConstant(), proposal.getAmount(), this);
		history.add(bet);
		return bet;
	}

	public void roundEnd() {
	}

	public void roundStart() {
	}

	public void win(int amount) {
		money += amount;
	}

	public void gameEnd() {
	}

	public int getMoney(){
		return money;
	}

	public List<Bet> getHistory(){
		return history;
	}
}
